package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Settlecategory;
import com.foreknow.service.SettlecategoryService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/settlecategory")
public class SettlecategoryController {
    @Autowired
    private SettlecategoryService settlecategoryService;
    @RequestMapping(value = "/findSettleByPage",method = RequestMethod.GET)
    public String findSetByPage(@RequestParam Map<String,String> map) throws JsonProcessingException {
        //获取参数
        String query = map.get("query");
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Settlecategory> settlecategoryList = settlecategoryService.findAll(query);
        PageInfo<Settlecategory> info = new PageInfo<>(settlecategoryList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("settleList",settlecategoryList);
        if (settlecategoryList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteSettleById",method = RequestMethod.DELETE)
    public String deleteReg(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(settlecategoryService.deleteById(id)){
            hs.put("meta", new Constance("删除成功", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteSettlesById", method = RequestMethod.POST)
    public String deleteRegs(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = settlecategoryService.deleteByIds(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/insertSettle",method = RequestMethod.POST)
    public String addReg(@RequestBody Settlecategory settlecategory)throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        if(settlecategoryService.insertInfo(settlecategory)){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/findSettleById",method = RequestMethod.GET)
    public String findRegById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Settlecategory settlecategory = settlecategoryService.findById(id);
        if(settlecategory!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", settlecategory);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateSettle", method = RequestMethod.PUT)
    public String updateReg(@RequestBody Settlecategory settlecategory) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(settlecategoryService.updateInfo(settlecategory)){
            hs.put("meta", new Constance("修改成功", 200));
        }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
